<?
	   layout("/inc/win_layout.html",{title:sysTitle}){
    ?>
    <div id="showMes"></div>
<div class="row" style="min-height: auto;padding-bottom: 20px;">
	<div class="col-lg-12">
		<div class="panel">
			<div class="panel-body pan" style="height: 600px">
				<form action="#" class="form-horizontal">
					<div class="form-body pal" style="width:auto; height: auto; margin-bottom: 0px;">

						<div style="width:700px;  margin:0 20px 0 20px; padding-bottom: 10px;">
							<label for="inputUsername" class="col-md-3 control-label">数据源名称
								<span class='require' id="zhongwenName">*</span> </label>
							<div class="col-md-9">
								<div class="input-icon">
									<input id="name" type="text" class="form-control"  />
								</div>
							</div>
						</div>
						
						<div style="width:700px;  margin:0 20px 0 20px; padding-bottom: 10px;">
							<label for="inputUsername" class="col-md-3 control-label">数据源描述
								<span class='require' id="zhongwenName">*</span> </label>
							<div class="col-md-9">
								<div class="input-icon">
									<input id="description" type="text" class="form-control"  />
								</div>
							</div>
						</div>
						
						<div style="width:700px;  margin:0 20px 0 20px;padding-bottom: 10px;">
							<label for="inputUsername" class="col-md-3 control-label">数据源类型
								<span class='require' id="leixing">*</span> </label>
							<div class="col-md-9">
								<div class="input-icon">
									<select id="sourceType" class="form-control" onchange="getSourceType(this);">
										<option value="excel">excel</option>
										<option value="json">json</option>
										<option value="txt">txt</option>
										<option value="xml">xml</option>
										<option value="mysql">mysql</option>
										<option value="access">access</option>
										<option value="oracle">oracle</option>
										<option value="sqlserver">sqlserver</option>
										<option value="mongodb">mongodb</option>
									</select>
								</div>
							</div>
						</div>
						
						<div id="mysql_oracle_sqlserver_mongdb" style="display: none;">
							<div style="width:700px;  margin:0 20px 0 20px; padding-bottom: 10px;">
								<label for="inputUsername" class="col-md-3 control-label">数据源连接
									<span class='require' id="leixing">*</span>
								</label>
								<div class="col-md-9">
									<div class="input-icon">
										<input id="url" type="text" class="form-control"  />
									</div>
								</div>
							</div>
							<div style="width:700px;  margin:0 20px 0 20px; padding-bottom: 10px;">
								<label for="inputUsername" class="col-md-3 control-label">用户名
									<span class='require' id="leixing">*</span> </label>
								<div class="col-md-9">
									<div class="input-icon">
										<input id="user" type="text" class="form-control"  />
									</div>
								</div>
							</div>
						
							<div style="width:700px;  margin:0 20px 0 20px; padding-bottom: 10px; ">
								<label for="inputUsername" class="col-md-3 control-label">密码
									<span class='require' id="leixing">*</span> </label>
								<div class="col-md-9">
									<div class="input-icon">
										<input id="password" type="text" class="form-control" style="width:440px;height:30px;float:left"/>
										<button type="button" class="btn btn-warning btn-square" style="width:80px;height:30px;" onclick="connectionTest();">连接测试</button>
									</div>
								</div>
							</div>
						</div>
						<div id="access" style="display: none;">
							<div style="width:700px;  margin:0 20px 0 20px; padding-bottom: 10px;">
								<label for="inputUsername" class="col-md-3 control-label">数据库名称
									<span class='require' id="leixing">*</span>
								</label>
								<div class="col-md-9">
									<div class="input-icon">
										<input id="url" type="text" class="form-control"  />
									</div>
								</div>
							</div>
							<div style="width:700px;  margin:0 20px 0 20px; padding-bottom: 10px;">
								<label for="inputUsername" class="col-md-3 control-label">用户名
									<span class='require' id="leixing">*</span> </label>
								<div class="col-md-9">
									<div class="input-icon">
										<input id="user" type="text" class="form-control"  />
									</div>
								</div>
							</div>
						
							<div style="width:700px;  margin:0 20px 0 20px; padding-bottom: 10px;">
								<label for="inputUsername" class="col-md-3 control-label">密码
									<span class='require' id="leixing">*</span> </label>
								<div class="col-md-9">
									<div class="input-icon">
										<input id="password" type="text" class="form-control"/>
									</div>
								</div>
							</div>
							<div style="width:700px;  margin:0 20px 0 20px; padding-bottom: 10px;">
								<label for="inputUsername" class="col-md-3 control-label">编码
									<span class='require' id="leixing">*</span> </label>
								<div class="col-md-9">
									<div class="input-icon">
										<input id="coding" type="text" class="form-control" style="width:340px;height:30px;float:left" />
										<button type="button" class="btn btn-warning btn-square" style="width:80px;height:30px;" onclick="connectionTest();">连接测试</button>
									</div>
								</div>
							</div>
						</div>
						
						<div style="width:700px;  margin:0 20px 0 20px; padding-bottom: 10px;">
							<label for="inputUsername" class="col-md-3 control-label">状态
								<span class='require' id="paixu">*</span> </label>
							<div class="col-md-9">
								<div class="input-icon">
									<select id="status" class="form-control">
										<option value="1">可使用</option>
										<option value="2">禁用</option>
									</select>
								</div>
							</div>
						</div>
						<input id="id" type="hidden" class="form-control" />
						
					</div>	
				</form>
			</div>
		</div>
	</div>
</div>
<?} ?>
<script>
	var index1 = window.parent.getlay();
	$("#id").val(index1);
	$(function() {
	 	$("#mask").hide();    
		if (index1 != 0) {
			demo1();
		}
	});
	function Page() {
		if($.trim($("#name").val()).length<1) {
			layer.msg("数据源名称不能为空！", {icon: 7});
	       	return null;
  	    }
       if($.trim($("#description").val()).length<1) {
    	   layer.msg("数据源描述不能为空！", {icon: 7});
			return null;
       }
       if($.trim($("#description").val()).length<1) {
    	   layer.msg("数据源名称不能为空！", {icon: 7});
       		return null;
       }
    	var sourceType=$("#sourceType").val();
		var url;
		var user;
		var password;
		var coding;
		if(sourceType=="access"){
			url=$("#access #url").val();
			user=$("#access #user").val();
			password=$("#access #password").val();
			coding=$("#access #coding").val();
		}else if(sourceType=="mysql" || sourceType=="oracle" || sourceType=="sqlserver" || sourceType=="mongodb"){
			url=$("#mysql_oracle_sqlserver_mongdb #url").val();
			user=$("#mysql_oracle_sqlserver_mongdb #user").val();
			password=$("#mysql_oracle_sqlserver_mongdb #password").val();
			coding="";
		}else{
			url="";
			user="";
			password="";
			coding="";
		}
		if(sourceType == "mysql" || sourceType == "oracle" || sourceType == "sqlserver" || sourceType == "mongodb"){
       		if(url.length<1) {
       			layer.msg("数据源链接不能为空！", {icon: 7});
       			return null;
       		}
       		if(user.length<1) {
       			
       			return null;
       		}
       		if(password.length<1) {
       			layer.msg("密码不能为空！", {icon: 7});
       			return null;
       		}
      	}
		 
        if(sourceType == "access"){
        	if(url.length<1) {
        		layer.msg("数据库不能为空！", {icon: 7});
        		return null;
        	}
        	if(user.length<1) {
        		layer.msg("用户名不能为空！", {icon: 7});
        		return null;
        	}
        	if(password.length<1) {
        		layer.msg("密码不能为空！", {icon: 7});
        		return null;
        	}
        }
		
		
		$.ajax({
			type : "post",
			url : "./addEditDataHandlingSource.do",
			data : {
				"id" : index1,
				"name" : $("#name").val(),
				"description" : $("#description").val(),
				"sourceType" : sourceType,
				"url" : url,
				"user" : user,
				"password" : password,
				"status": $("#status").val(),
				"coding": coding,
			},
			dataType : "json",
			async : false,
			success : function(data) {
				var index = parent.layer.getFrameIndex(window.name);
				window.parent.get();
				layer.msg("提交成功！", {icon: 6});
				parent.layer.close(index);
			},
			error : function(data) {
				layer.msg("请求失败！", {icon: 2});
			}
		});
	}

	function demo1() {
		$.getJSON('./dataHandlingSourceEditShow.do', {
			id : index1
		//向服务端传的参数，此处只是演示
		}, function(res) {
			//此处仅仅是为了演示变化的内容
			$("#id").val(res[0].id);
			$("#name").val(res[0].name);
			$("#description").val(res[0].description);
			$("#sourceType").val(res[0].sourceType);
			$("#status").val(res[0].status);
			
			var sqla = document.getElementById("mysql_oracle_sqlserver_mongdb");
			var access = document.getElementById("access");
			if(res[0].sourceType == "access"){
     			sqla.style.display="none";
     			access.style.display="";
				$("#access #url").val(res[0].url);
				$("#access #user").val(res[0].user);
				$("#access #password").val(res[0].password);
				$("#coding").val(res[0].coding);
			}else if(res[0].sourceType=="mysql" || res[0].sourceType=="oracle" || res[0].sourceType=="sqlserver" || res[0].sourceType=="mongodb"){
				sqla.style.display="";
     			access.style.display="none";
				$("#mysql_oracle_sqlserver_mongdb #url").val(res[0].url);
				$("#mysql_oracle_sqlserver_mongdb #user").val(res[0].user);
				$("#mysql_oracle_sqlserver_mongdb #password").val(res[0].password);
				$("#coding").val(res[0].coding);
			}else{
				sqla.style.display="none";
     			access.style.display="none";
				$("#access #url").val("");
				$("#access #user").val("");
				$("#access #password").val("");
				$("#mysql_oracle_sqlserver_mongdb #url").val("");
				$("#mysql_oracle_sqlserver_mongdb #user").val("");
				$("#mysql_oracle_sqlserver_mongdb #password").val("");
				$("#coding").val("");
			}
			
		});
	};
	function getSourceType(type){
		if(type.value=="mysql"){
			$("#mysql_oracle_sqlserver_mongdb #url").attr("value","jdbc:mysql://ip:3306/数据库名称?useUnicode=true&amp;characterEncoding=UTF-8");
		}
		if(type.value=="access"){
			$("#access #url").attr("value","数据库名称");
		}
		if(type.value=="oracle"){
			$("#mysql_oracle_sqlserver_mongdb #url").attr("value","jdbc:Oracle:thin:@ip:1521:orcl");
		}
		if(type.value=="sqlserver"){
			$("#mysql_oracle_sqlserver_mongdb #url").attr("value","jdbc:jtds:sqlserver://localhost:1433/数据库名称;instance=非默认实例名");
		}
		if(type.value=="mongodb"){
			$("#mysql_oracle_sqlserver_mongdb #url").attr("value","ip:数据库名称");
		}
		
		
		var sqla = document.getElementById("mysql_oracle_sqlserver_mongdb");
		var access = document.getElementById("access");
		if(type.value=="mysql" || type.value == "oracle" || type.value == "sqlserver" || type.value == "mongodb"){
     		sqla.style.display="";
     		access.style.display="none";
		}
		if(type.value == "access"){
     		sqla.style.display="none";
     		access.style.display="";
		}
		if(type.value=="txt" || type.value=="excel" || type.value=="json" || type.value=="xml"){
     		sqla.style.display="none";
     		access.style.display="none";
		}
	}
	
	function connectionTest(){
		var sourceType=$("#sourceType").val();
		var url;
		var user;
		var password;
		var coding;
		if(sourceType=="access"){
			url=$("#access #url").val();
			user=$("#access #user").val();
			password=$("#access #password").val();
			coding=$("#access #coding").val();
		}else if(sourceType=="mysql" || sourceType=="oracle" || sourceType=="sqlserver" || sourceType=="mongodb"){
			url=$("#mysql_oracle_sqlserver_mongdb #url").val();
			user=$("#mysql_oracle_sqlserver_mongdb #user").val();
			password=$("#mysql_oracle_sqlserver_mongdb #password").val();
			coding="";
		}else{
			url="";
			user="";
			password="";
			coding="";
		}
 		 var layerIndex=layer.open({
					  type: 1,
					  title: false, //不显示标题栏
					  closeBtn: false,
					  area: '200px;',
					  shade: 0.5,
					  id: 'LAY_layuipro', //设定一个id，防止重复弹出
					  resize: false,
					  btnAlign: 'c',
					  moveType: 1, //拖拽模式，0或者1
					  content: '<div style="padding: 30px; line-height: 22px; background-color: #cccccc; color: #000;"><img src="static/vendors/pageloader/images/loader4.GIF">正在连接......</div>',
					});
		 $.ajax({
			type : "post",
			url : "./dataHandlingSourceConnectionTest.do",
			data : {
				"id" : index1,
				"name" : $("#name").val(),
				"description" : $("#description").val(),
				"sourceType" : sourceType,
				"url" : url,
				"user" : user,
				"password" : password,
				"status": $("#status").val(),
				"coding": coding,
			},
			dataType : "json",
			async : true,
			success : function(data) {
				layer.close(layerIndex);
				var jsonO = eval(data);
				if(jsonO[0].info =="1"){
					
					layer.msg("连接成功!", {icon: 6});
				}else{
					layer.msg("连接失败!", {icon: 5});
				}
			},
			error : function(data) {
				layer.close(layerIndex);
				layer.msg("连接失败!", {icon: 2});
			}
		});
	}
</script>


